package homework;

import java.util.Stack;

//力扣682题、棒球比赛（简单）
public class calPoints {
    public int calPoints(String[] ops) {
        Stack<Integer> stack = new Stack<>();
        int sum = 0;
        for (int i = 0; i < ops.length; i++){
            String tmp = ops[i];
            if (tmp.equals("C")){
                stack.pop();
            }else if (tmp.equals("D")){
                stack.push(stack.peek() * 2);
            }else if (tmp.equals("+")){int num1 = stack.pop();
                int num2 = stack.peek();
                stack.push(num1);
                stack.push(num1 + num2);
            }else {
                stack.push(Integer.parseInt(tmp));
            }
        }
        while (!stack.empty()){
            sum += stack.pop();
        }
        return sum;
    }
}
